package cdu.notFound.Dao.impl;



import cdu.notFound.Dao.ShopCartDao;
import cdu.notFound.model.Goods;
import cdu.notFound.model.User;
import cdu.notFound.utils.DatabaseConnection;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ShopCartDaoImpl implements ShopCartDao {
	private Connection conn = null;
    private PreparedStatement pstmt = null;

    //获取mysql连接
    public ShopCartDaoImpl() {
        try {
            this.conn = new DatabaseConnection().getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取user购物车物品数量
    public Integer getShopCartNum(int id){
        String sql = "SELECT count(goodsId) FROM ShoppingCart WHERE userId=?";
        try {
            this.pstmt = this.conn.prepareStatement(sql);
            this.pstmt.setInt(1, id);
            ResultSet rs = this.pstmt.executeQuery();
            if(rs.next()){
                return rs.getInt(1);
            }else{
                return 0;
            }
        } catch (SQLException e) {
            return 0;
        }
    }
    //增加一条购物车记录
    public boolean doSaveShoppingCart( int goodsId,int userId){
        boolean flag = false;
        String sql="select id from shoppingcart where userId="+userId+" and goodsId="+goodsId;
        try {
            //查询是否已经存在
            this.pstmt = this.conn.prepareStatement(sql);
            ResultSet rs = null;
            rs = this.pstmt.executeQuery();
            if(rs.next()){
                return false;
            }
            sql = "INSERT INTO shoppingcart(goodsId,userId) VALUES (?,?)";
            this.pstmt = this.conn.prepareStatement(sql);
            pstmt.setInt(1, goodsId);
            pstmt.setInt(2, userId);
            if (this.pstmt.executeUpdate() > 0) {
                flag = true;
            }
            rs.close();
            this.pstmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    //查找user所有购物车内物品
    public List<Goods> findGoodsByUser(User user){
        int userId=user.getId();
        List<Goods> goods = new ArrayList<>();
        String sql = "select id,num,content,type_id,image,producter_id,price,create_date,name from `goods` where id=any(SELECT goodsId from `shoppingcart`  where userId=?) order by create_date desc";
        try {
            this.pstmt = this.conn.prepareStatement(sql);
            this.pstmt.setInt(1,userId);
            ResultSet rs = this.pstmt.executeQuery();
            while (rs.next()) {
                Goods good = new Goods();
                good.setId(rs.getInt(1));
                good.setNum(rs.getInt(2));
                good.setContent(rs.getString(3));
                good.setType_id(rs.getInt(4));
                good.setImage(rs.getString(5));
                good.setProducter_id(rs.getInt(6));
                good.setPrice(rs.getFloat(7));
                Timestamp timeStamp=rs.getTimestamp(8);
                good.setName(rs.getString(9));
                Date date=new Date(timeStamp.getTime());
                good.setCreatDate(date);
                goods.add(good);
            }
        rs.close();this.pstmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return goods;
    }
    //移除购物车的一个物品
    public boolean removeList(int goodId,int userId){
		Boolean flag=false;
		String sql="Delete from shoppingcart where goodsId=? and userId=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1, goodId);
            pstmt.setInt(2, userId);
            if (this.pstmt.executeUpdate() > 0) {
                flag = true;
            }
            this.pstmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    } 

    
	public void close(){
		if(this.conn != null){
			try{
				this.conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}
